package fivestar.ui.main;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdesktop.swingx.JXPanel;

import fivestar.ui.images.FiltroImagenes;
import fivestar.ui.images.PrevisualizadorImagenes;

public class Background extends javax.swing.JDialog implements ActionListener{ 
	private static final long serialVersionUID = -5574134571740380768L;
	private JPanel pnlBackground;
	private JButton btnChooseImage;
	private JButton btnSave;
	private JButton btnCancel; 
    JXPanel pnlPreviousImage;
    JLabel lblBackgroundImage;
    
    private String ruta;
    java.awt.Frame frame;    
  
	public Background(Frame parent, boolean modal, String ruta) {
		super(parent,modal);
		initComponents();
		this.setLocationRelativeTo(null);
	    frame = parent;
		
	}
	
	private void initComponents() {
		try {

			pnlBackground = new JPanel();
			getContentPane().add(pnlBackground, BorderLayout.CENTER);
			pnlBackground.setLayout(null);

			btnChooseImage = new JButton();
			btnChooseImage.addActionListener(this);
			pnlBackground.add(btnChooseImage);
			btnChooseImage.setText("Escoger Imagen");
			btnChooseImage.setBounds(20, 25, 132, 23);

			btnCancel = new JButton();
			btnCancel.addActionListener(this);
			pnlBackground.add(btnCancel);
			btnCancel.setText("Cancelar");
			btnCancel.setBounds(20, 53, 132, 23);

			btnSave = new JButton();
			btnSave.addActionListener(this);
			pnlBackground.add(btnSave);
			btnSave.setText("Guardar");
			btnSave.setBounds(20, 81, 132, 23);
			
			pnlPreviousImage = new JXPanel();
			pnlPreviousImage.setBounds(175, 11, 477, 326);
			pnlPreviousImage.setBorder(BorderFactory.createTitledBorder(""));
			pnlBackground.add(pnlPreviousImage);
			pnlPreviousImage.setLayout(null);
			
			lblBackgroundImage = new JLabel("");
			lblBackgroundImage.setBounds(185, 16, 458, 311);
			pnlBackground.add(lblBackgroundImage);

			this.setSize(681, 387);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
	        java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	            	Background dialog = new Background(null,true,"");
	                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
	                    public void windowClosing(java.awt.event.WindowEvent e) {
	                        System.exit(0);
	                    }
	                });
	                dialog.setVisible(true);
	            }
	        });
	    }

	@Override 
	public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnChooseImage) {
            JFileChooser dialogo = new JFileChooser();
            dialogo.addChoosableFileFilter(new FiltroImagenes());
            dialogo.setAcceptAllFileFilterUsed(false);
            dialogo.setAccessory(new PrevisualizadorImagenes(dialogo));
            int valorRetorno = dialogo.showDialog(this,
                    "Abrir");
            if (valorRetorno == JFileChooser.APPROVE_OPTION) {
                ruta = dialogo.getSelectedFile().getAbsolutePath();
                try {
                    File archivo = new File(getRuta());
                    BufferedImage img = ImageIO.read(archivo);
                    ImageIcon imagen = new ImageIcon(img.getScaledInstance(pnlPreviousImage.getWidth(), pnlPreviousImage.getHeight(), Image.SCALE_FAST));
                    //ImagePainter painter = new ImagePainter(img);
                    lblBackgroundImage.setIcon(imagen);
                    //panelImagenPrevia.add(imagenFondo);
                    
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (e.getSource() == btnSave) {
        	this.setVisible(false);
        }
        if (e.getSource() == btnCancel) {
             ruta = "";
            this.dispose();
        } 
    }
	 
    public String getRuta() {
        return ruta;
    } 
    public void setRuta(String ruta) {
        this.ruta = ruta;
    }

}
